<?php defined('InShopNC') or exit('Access Invalid!');?>
<?php echo BAIDUMAP_API_KEY; ?>	
<script type="text/javascript" src="<?php echo RESOURCE_PATH;?>/js/jquery.validation.min.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATES_PATH;?>/js/area_array.js" charset="utf-8"></script>
<script type="text/javascript" src="<?php echo TEMPLATES_PATH;?>/js/common_select.js?v=<?php echo VERSION;?>" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="<?php echo TEMPLATES_PATH;?>/css/brand.css?v=<?php echo VERSION;?>"/>

<header>
    <a href="javascript:back(true);" class="hBack float-left"></a>
    <h4 class="hTxt float-left text-center text-white"><?php echo $output['setting']['brand_name'];?>-品牌加盟</h4>
    <a href="javascript:;" id="btnJdkey" class="hHlep float-right"></a>
</header>

<div class="brand_alliance">
	<form action="index.php?act=brand_alliance&op=alliance_add" method="post" id="formcompany">
		<dl class="relation_add">
			<dd><select name="province_id" id="province_id"></select></dd>
			<dd><input type="text" name="address" id="address" class="text" value="" placeholder="详细地址" /></dd>
			<dd><input type="text" name="true_name" id="true_name" class="text" value="" placeholder="姓名" /></dd>
			<dd><input type="text" name="mobile" id="mobile" class="text" value="" placeholder="手机号码" /></dd>
			<dd><input type="text" name="store_name" id="store_name" class="text" value="" placeholder="商城名称" /></dd>
                           <dd>
                                <input type="checkbox" name="agreement" id="agreement" value="1" />
                                <a url="index.php?act=brand_alliance&op=treaty&s_id=<?php echo $output['setting']['s_id'];?>" onClick="pclicked(this);">同意服务协议</a>
                          </dd>
			<dd>
				<div class="btn_area">
					<div><input type="submit" value="加盟" class="btn" id="subForm" /></div>
					<div><input type="button" class="btn" value="查询" onclick="getAddressPoint();"/></div>
				</div>
			</dd>
		</dl>
		<input type="hidden" id="pca_info" name="pca_info" value="" />
		<input type="hidden" id="lng" name="lng" value="" />
		<input type="hidden" id="lat" name="lat" value="" />
		<input type="hidden" name="brand_id" value="<?php echo $output['setting']['brand_id'];?>" />
		<input type="hidden" name="brand_name" value="<?php echo $output['setting']['brand_name'];?>" />
	</form>
 
	<div id="allmap"></div>
</div>

<script type="text/javascript">
	var DISTANCE = <?php echo $output['setting']['distance'];?>;
	var can = false;
	
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	var point = new BMap.Point(116.331398,39.897445);	 //默认定位城市为北京
	map.centerAndZoom(point,12);
	//打开时先根据IP定位城市
	function myFun(result){
		var cityName = result.name;
		map.setCenter(cityName);
	}
	var myCity = new BMap.LocalCity();
	myCity.get(myFun);
	
	//获取输入地址的坐标
	function getAddressPoint(){
		map.clearOverlays();
		map.closeInfoWindow();
		var myGeo = new BMap.Geocoder();
		var searchTxt = $('#province_id option:selected').text()+$('#city_id option:selected').text()+$('#area_id option:selected').text()+$('#address').val(); 
		$('#pca_info').val($('#province_id option:selected').text()+$('#city_id option:selected').text()+$('#area_id option:selected').text());
		myGeo.getPoint(searchTxt, function (point) {
			if (point) {
				//重新定位
				map.centerAndZoom(point,14);
				var marker = new BMap.Marker(point);
				map.addOverlay(marker);
				//绘制圆形区域
				var circle = new BMap.Circle(point,DISTANCE,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.2, strokeOpacity: 0.2});
				map.addOverlay(circle);
				//标记信息
				var info = "<p style=\'margin:0;line-height:1.5;font-size:13px;text-indent:2em\'>"+searchTxt+"</p>";
				var infoWindow = new BMap.InfoWindow(info);
				map.openInfoWindow(infoWindow,point);
				marker.addEventListener("click", function(){
					map.openInfoWindow(infoWindow,point);
				});
				//查询附近已加盟的数据
				$.getJSON('index.php?act=brand_alliance&op=get_points',{brand_id:$('input[name=brand_id]').val(),mobile:$('input[name=mobile]').val(),brand_name:'<?php echo $output['setting']['brand_name'];?>',lng:point.lng,lat:point.lat,distance:DISTANCE},function(res){
					if(res.state){
						for(var i in res.list){
							var pointB = new BMap.Point(res.list[i].lng,res.list[i].lat);
							var marker = new BMap.Marker(pointB);
							map.addOverlay(marker);
							//标记信息
							var info = "<h4 style=\'margin:0 0 5px 0;padding:10px 0 0 0\'>"+res.list[i].store_name+"</h4><p style=\'margin:0;line-height:1.5;font-size:13px;text-indent:2em\'>"+res.list[i].pca_info+res.list[i].address+"</p>";
							var infoWindow = new BMap.InfoWindow(info);
							map.openInfoWindow(infoWindow,pointB);
						}
						can = false;
						alert('对不起，该品牌在本区域已有加盟店，您可以看看其他的热门品牌。');
                                                     pclicked('index.php?act=brand');
					}else{
						can = true;
						$('#subForm').show();
						alert('恭喜！当前地区可以加盟该品牌，您已屏蔽 '+res.num+' 家店家。');
					}
				});
				$('#lng').val(point.lng);
				$('#lat').val(point.lat);
			}else{
				alert('定位失败');
			}
		});
	}
	
	
 //表单验证
$(function(){
//设置地图区域高度宽度一致
$('#allmap').height($('#allmap').width());

// 手机号码验证   
jQuery.validator.addMethod("isMobile", function(value, element) {   
    var tel = /^1[3|5|7|8]\d{9}$/;
    return this.optional(element) || (tel.test(value));
}, "手机号码输入错误");
//姓名验证
jQuery.validator.addMethod("isName", function(value, element) {
	return this.optional(element) || /^[\u4E00-\u9FA5]{2,15}$/.test(value);
});
        $('#formcompany').validate({
            submitHandler:function(form){
			  var url = $('#formcompany').attr('action');
			  $.post('<?php echo SiteUrl;?>/app/'+url,$("form#formcompany").serialize(),function(data){
					Jump(data);
			  },'json');
             },
            rules : {
                province_id:{
                    required:true
                },
                city_id:{
                    required:true
                },
                area_id:{
                    required:true
                },
			  address:{
                    required:true
                },
			  true_name:{
                    required:true,
				  isName : true
                },
			  mobile:{
                    required:true,
				  isMobile : true
                },
			  store_name:{
                    required:true
                },
                agreement:{
                    required:true
                },
            },
            messages : {
                province_id:{
                    required:'请选择省份'
                },
                city_id:{
                    required:'请选择城市'
                },
                area_id:{
                    required:'请选择地区'
                },
			  address:{
                    required:'请填写详细地址'
                },
			   true_name:{
                    required:'请填写姓名',
				  isName : '姓名输入错误'
                },
			  mobile:{
                    required:'请填写手机号码',
				  isMobile : '手机号码输入错误'
                },
			  store_name:{
                    required:'请填写店名'
                },
                agreement:{
                    required:'请同意服务协议'
                },
            }
        });
				
				
		var area_select = $("#province_id");
		areaInit(area_select,0);//初始化地区
		// 选择地区
		$("#province_id").change(provinceChange);
		function provinceChange(){
			can = false;
			$('#subForm').hide();
		   // 删除后面的select
		   $(this).nextAll("select").remove();
		   if (this.value > 0){
				var text = $(this).get(0).options[$(this).get(0).selectedIndex].text;
				var area_id = this.value;
				var EP = new Array();
				EP[1]= true;EP[2]= true;EP[9]= true;EP[22]= true;EP[34]= true;EP[35]= true;
				if(typeof(nc_a[area_id]) != 'undefined'){//数组存在
					var areas = new Array();
					var option = "";
					areas = nc_a[area_id];
				/*if (typeof(EP[area_id]) == 'undefined'){
					option = "<option value=''>-请选择-</option>";
				}*/
				$("<select name='city_id'  id='city_id'>"+option+"</select>").insertAfter(this);
				$(this).next("select").append("<option value=''>-请选择-</option>");
					for (i = 0; i <areas.length; i++){
						$(this).next("select").append("<option value='" + areas[i][0] + "'>" + areas[i][1] + "</option>");
					}
				}
		   }	 	
		}
    $('#city_id').live('change', function() {
		can = false;
		$('#subForm').hide();
        // 删除后面的select
        $(this).nextAll("select").remove();
        if (this.value > 0){
            var text = $(this).get(0).options[$(this).get(0).selectedIndex].text;
            var area_id = this.value;
            var EP = new Array();
            EP[1]= true;EP[2]= true;EP[9]= true;EP[22]= true;EP[34]= true;EP[35]= true;
            if(typeof(nc_a[area_id]) != 'undefined'){//数组存在
                var areas = new Array();
                var option = "";
                areas = nc_a[area_id];
                if (typeof(EP[area_id]) == 'undefined'){
                    option = "<option value=''>-请选择-</option>";
                }
                $("<select name='area_id' id='area_id'>"+option+"</select>").insertAfter(this);
                for (i = 0; i <areas.length; i++){
                    $(this).next("select").append("<option value='" + areas[i][0] + "'>" + areas[i][1] + "</option>");
                }
            }
        }
    });
	$('#area_id').live('change',function(){can = false;$('#subForm').hide();});
	$('#address').live('change',function(){can = false;$('#subForm').hide();});
});
</script>
